package com.home.job.zh1.job1;


import java.util.Scanner;

public class NumberToEnglish {
    private static final String[] ENGLISH_UNIT = {
            "hundred", "thousand", "million"
    };

    private static final String[] ENGLISH_NUMBER = {
            "zero",
            "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
            "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
            "twenty", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位",
            "thirty", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位",
            "forty", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位",
            "fifty", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位",
            "sixty", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位",
            "seventy", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位",
            "eighty", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位", "占位",
            "ninety"
    };

    private static final String REG = "^-?\\d+$";

    public static void main(String[] args) {
        System.out.print("请输入[-999999999, 999999999]内的一个数：");
        Scanner scan = new Scanner(System.in);
        String number = "";
        if (scan.hasNext()) {
            number = scan.next();
        }
        System.out.println(toEnglish(number));
        scan.close();
    }

    public static String toEnglish(String number) {
        // 结果保存
        StringBuilder english = new StringBuilder();

        if (null == number){
            return "please enter a number";
        }

        if (!number.matches(REG)) {
            return String.format("%s is not number", number);
        }

        // 小于零 首尾添加 negative 标识
        if (Integer.parseInt(number) < 0) {
            english.append("negative").append(" ");
            number = number.substring(1);
        }

        if (Integer.parseInt(number) == 0) {
            return ENGLISH_NUMBER[0];
        } else if (number.length() > 9){
            return "too big";
        }

        int count = (number.length() % 3 == 0) ? (number.length() / 3) : (number.length() / 3 + 1);
        // 按照3位分段  1 000 000
        String[] group = new String[count];
        for (int i = number.length(), j = group.length - 1; i > 0; i -= 3) {
            group[j--] = number.substring(Math.max(i - 3, 0), i);
        }

        for (int i = 0; i < count; i++) {
            int v = Integer.parseInt(group[i]);
            if (v >= 100) {
                english.append(ENGLISH_NUMBER[v / 100]).append(" ").append(ENGLISH_UNIT[0]).append(" ");
            }
            // 获取百位数
            v = v % 100;
            if (v == 0) {
                english.append("and ");
            }
            if (v < 20 || v % 10 == 0) {
                // 如果小于20或10的整数倍，直接取基本数词表的单词
                english.append(ENGLISH_NUMBER[v]).append(" ");
            } else {
                // 否则取10位数词，再取个位数词
                english.append(ENGLISH_NUMBER[v - v % 10]).append(" ");
                english.append(ENGLISH_NUMBER[v % 10]).append(" ");
            }
            if (i != count - 1) {
                // 百位以上的组追加相应的单位
                english.append(ENGLISH_UNIT[count - 1 - i]).append(" ");
            }
        }
        return english.toString().trim();
    }
}
